package com.donn.securitycontrolplatform.personinfo.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.multiinone.brm.domain.CardInfo;
import com.donn.securitycontrolplatform.common.Result;
import com.donn.securitycontrolplatform.db2.mapper.PersonInfoMapper;
import com.donn.securitycontrolplatform.emum.RESULT_CODE;
import com.donn.securitycontrolplatform.expection.BusinessException;
import com.donn.securitycontrolplatform.personimg.entity.PersonImg;
import com.donn.securitycontrolplatform.personimg.service.IPersonImgService;
import com.donn.securitycontrolplatform.personinfo.entity.PersonInfo;
import com.donn.securitycontrolplatform.personinfo.service.IPersonInfoService;
import com.donn.securitycontrolplatform.taginfo.entity.Taginfo;
import com.donn.securitycontrolplatform.taginfo.service.ITaginfoService;
import com.donn.securitycontrolplatform.user.entity.DaHuaPerson;
import com.donn.securitycontrolplatform.user.service.ITUserService;
import com.donn.securitycontrolplatform.utils.DaHuaUtil;
import com.donn.securitycontrolplatform.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * LOC 用户 服务实现类
 * </p>
 *
 * @author zhenglx
 * @since 2022-06-23
 */
@Service
@DS("db2")
public class PersonInfoServiceImpl extends ServiceImpl<PersonInfoMapper, PersonInfo> implements IPersonInfoService {
    @Autowired
    IPersonImgService personImgService;
    @Autowired
    ITUserService userService;
    @Autowired
    DaHuaUtil daHuaUtil;
    @Autowired
    ITaginfoService taginfoService;


    @Override
    public PersonInfo getPersonByNameAndIdNo(String name, String idNo) {
        return baseMapper.getPersonByNameAndIdNo(name, idNo);
    }

    @Override
    public Result getPage(Page<PersonInfo> page) {
        IPage<PersonInfo> dto = baseMapper.getPage(page);
        return Result.success(dto);
    }

    @Override
    public Result add(PersonInfo personInfo) throws BusinessException, ClientException {
        //保存图片到指定目录下
        if (ObjectUtils.isEmpty(personInfo.getFile())) throw new BusinessException(RESULT_CODE.PERSON_IMG_NOT_FOND);
        String filePath = FileUtil.updateFile(personInfo.getFile(), PersonInfo.PERSON_IMG_PATH + PersonInfo.PERSON_IMG_CHILD_PATH);
        //保存数据
        save(personInfo);
        //保存用户图片关联数据
        PersonImg personImg = new PersonImg();
        personImg.setPersonId(personInfo.getId());
        personImg.setImgPath(filePath);
        personImg.setAddEntranceGuard(personInfo.getAddEntranceGuard());
        personImg.setEndDate(personInfo.getEndDate());
        personImg.setStartDate(personInfo.getStartDate());
        personImgService.save(personImg);
        //大华创建人员信息
        Map<String, Object> map = daHuaUtil.addPersonAndCards(personInfo.getName(), personInfo.getIdno(), PersonInfo.PERSON_IMG_PATH + filePath, personInfo.getStartDate(), personInfo.getEndDate());
        List<CardInfo> cards = (List<CardInfo>) map.get("cards");
        String cardNumber = cards.get(0).getCardNumber();
        //同步大华摄像头白名单
        userService.synchronizeAdd(personInfo.getName(), personInfo.getIdno(), cardNumber);
        //同步大华门禁
        userService.authByPersonId((Long) map.get("personId"), 1L);
        //添加标签使用和人员关联
        Taginfo taginfo=taginfoService.getByTagId(personInfo.getTagNo());
        taginfo.setBindid(personInfo.getId());
        taginfo.setUsed(1);
        taginfoService.updateById(taginfo);


        return Result.success("");
    }

    @Override
    public Result updatePerson(PersonInfo personInfo) throws BusinessException, ClientException {
        //保存图片到指定目录下
        if (ObjectUtils.isEmpty(personInfo.getFile())) throw new BusinessException(RESULT_CODE.PERSON_IMG_NOT_FOND);
        String filePath = FileUtil.updateFile(personInfo.getFile(), PersonInfo.PERSON_IMG_PATH + PersonInfo.PERSON_IMG_CHILD_PATH);
        //保存数据
        updateById(personInfo);
        //保存用户图片关联数据
        PersonImg personImg = personImgService.getByPersonId(personInfo.getId());
        personImg.setImgPath(filePath);
        personImg.setAddEntranceGuard(personInfo.getAddEntranceGuard());
        personImg.setEndDate(personInfo.getEndDate());
        personImg.setStartDate(personInfo.getStartDate());
        personImgService.updateById(personImg);
        //同步大华摄像头
        DaHuaPerson person = new DaHuaPerson();
        person.setIdno(personInfo.getIdno());
        person.setId(personInfo.getId());
        person.setSex(personInfo.getSex());
        person.setName(personInfo.getName());
        userService.synchronizeUpdate(person);
        return Result.success("");

    }

    @Override
    public Result deletePerson(Integer personId) throws BusinessException, ClientException {
        //删除图片文件
        PersonImg byPersonId = personImgService.getByPersonId(personId);
        if (!ObjectUtils.isEmpty(byPersonId)) {
            File file = new File(PersonInfo.PERSON_IMG_PATH + byPersonId.getImgPath());
            if (file.exists()) {
                file.delete();
            }
        }
        //删除图片记录
        personImgService.removeById(byPersonId.getId());
        //删除人员记录
        removeById(personId);
        //删除大华白名单库数据
        userService.synchronizeDelete(personId);
        //删除大华门禁授权
        userService.deleteMj(personId);


        return Result.success("");
    }
}
